<template>
  <el-container>
    <el-header style="height: 40px">
      <el-row style="width: 1300px;margin: 0 auto;">
        <el-col :span="12">
          <div class="grid-content bg-purple">
          <ul class="left">
            <li>
              <el-icon  size="20px"><Star /></el-icon>
              收藏我们
            </li>
            <li @mouseover="mouseOver" @mouseout="mouseOut">
              <el-icon  size="20px"><User /></el-icon>
              关注我
              <img v-show="attention" src="http://lixinghe.meimeng.eu.org/flower/image/%E5%85%B3%E6%B3%A8%E6%88%91%E4%BA%8C%E7%BB%B4%E7%A0%81.png" style="width: 200px;height: 200px" alt="">
            </li>
            <li>
              <el-icon  size="20px"><Iphone /></el-icon>
              下载APP
            </li>
          </ul>
        </div>
        </el-col>
        <el-col :span="12"><div class="grid-content bg-purple-light">
          <ul class="right">
            <li v-if="store.state.userInfo.nickName">
              <a >你好 {{store.state.userInfo.nickName}}</a>
            </li>
            <li v-else @click="goTo('login1')">
              <a>客官 请登录</a>
            </li>
            <li v-if="store.state.userInfo.nickName" @click="onLogout()">
              <el-icon  size="20px"><CloseBold /></el-icon>
              <a>退出</a>
            </li>
            <li @click="goTo('login2')" v-else>
              <a >注册</a>
            </li>
            <li @click="goTo('/')">
              <el-icon  size="20px"><Tickets /></el-icon>
              <a >订单查询</a>
            </li>
            <li @click="goTo('/usercenter')" style="position:relative;">
              <el-icon  size="20px"><ChromeFilled /></el-icon>
              <a >会员中心</a>
              <div class="menu">
                <p>我的订单</p>
                <p>收货地址</p>
                <p>我的收藏</p>
                <p>我的钱包</p>
              </div>
            </li>
            <li  @click="goTo('/cart')">
              <el-icon  size="20px"><ShoppingTrolley /></el-icon>
              <a >购物车</a>
            </li>
          </ul>
        </div>
        </el-col>
      </el-row>
    </el-header>
  </el-container>
</template>

<script>
import {reactive, toRefs,onMounted,computed} from 'vue'
import {userInfo,logOut} from "@/api/login";
import { ElMessage } from 'element-plus'
import { useStore } from 'vuex' // 引入useStore 方法
import router from "@/router";
  // 定义数据
  export default {
    name:'nav',
    setup(){
      let data = reactive({
        nickName:'',
        attention: false,

        //鼠标移入
        mouseOver(){
          data.attention = true
        },
        mouseOut(){
          data.attention = false
        }
      })

      //进行路由跳转
      function goTo(path){
        if(path == 'login1'){
          router.push({
            name:'login1',
            query:{
              type:1
            }
          })
        }else if(path == 'login2'){
          router.push({
            name:'login1',
            query:{
              type:2
            }
          })
        }else {
          router.push(path)
        }

      }

      //接收store
      const store = useStore()


      onMounted(()=>{
        console.log();
        if (window.localStorage.getItem('token')) {
          userInfo().then(res =>{
            if(res.status == 200){
              console.log('用户信息获取成功~~');
              // console.log(res);
              store.commit('SET_USERINFO',res.data)
            }
          })
        }
      })

      //退出登录
      function onLogout(){

        // 向后端发送请求 删除redis里存储的数据
        logOut().then(res =>{
          if(res.status == 200){
            console.log('退出成功');
            //前端删除 local里的数据
            window.localStorage.removeItem('token')
            //删除vuex数据
            store.commit('SET_USERINFO',{})
          }
        })



      }




      return {
        ...toRefs(data),
        goTo,
        store,
        onLogout
      }
    }
}
</script>

<style scoped lang="scss">
  a {
    cursor: pointer;
  }
  body {
    margin: 0;
    padding: 0;
  }
  .el-header, .el-footer {
    background-color: #F3F5F7;
    color: #333;
    text-align: center;
    line-height: 40px;
  }
  .el-row {
    background-color: #F3F5F7;
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    background: #F3F5F7;
  }
  .bg-purple-light {
    background: #F3F5F7;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
  /*右边导航栏的样式*/
  ul {
    height: 40px;
    margin: 0;
    padding-left: 20px;
  }
  li {
    list-style-type: none;
    color: #C0C4CC;
    a {
      text-decoration: none;
      color: #C0C4CC;
    }
  }
  .font-icon {
    width: 2em;
    height: 2em;
    fill: currentColor;
    position: absolute;
    left: -30px;
    top: 6px;
    overflow: hidden;
  }
  .left {
    position: relative;
    padding-left: 50px;
    /* justify-content: space-between; */
    display: flex;
    align-items: center;
    li {
      cursor: pointer;
      /* width: 33.3%; */
      display: flex;
      justify-content: center;
      width: 120px;
      /* width: 100px; */
      align-items: center;
      list-style-type: none;
      transition: background-color 2s linear;
      /* float: left; */
      font-size: 12px;
      position: relative;
      &:hover {
        color: white;
        background:#9de1c6;
      }
    }
    img {
      z-index: 1;
      border: 1px solid gainsboro;
      position: absolute;
      left: -70px;
      top: 40px;
    }
  }
  .right {
    position: relative;
    padding-left: 50px;
    /* justify-content: space-between; */
    display: flex;
    align-items: center;
    li {
      cursor: pointer;
      /* width: 33.3%; */
      display: flex;
      justify-content: center;
      width: 120px;
      /* width: 100px; */
      align-items: center;
      list-style-type: none;
      /* float: left; */
      transition: background-color 2s linear;
      font-size: 12px;
      position: relative;
      &:hover {
        color: white;
        background:#9de1c6;
      }
      &:hover a{
        color: white;
      }
    }
    li:nth-of-type(4) .menu {
      /*css动画效果*/
      transition: all 1.5s;
      height: 0;
    }
    li:nth-of-type(4):hover .menu {
      display: block;
      height: 160px;
    }
  }
  .menu {
    position: absolute;
    overflow: hidden;
    top: 40px;
    z-index: 10;
    width: 120px;
    background: antiquewhite;
    border-radius: 0 0 10px 10px;
    p {
      margin: 0;
      border-top: 1px solid #c0c0c0;
      font-size: 14px;
      color: white;
      cursor: pointer;
    }
  }
</style>
